package com.zjlanyun.erp.rfid.m10.u8.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.zjlanyun.erp.MainApplication;
import com.zjlanyun.erp.R;
import com.zjlanyun.erp.rfid.m10.u8.reader.CMD;
import com.zjlanyun.erp.rfid.m10.u8.reader.model.ReaderSetting;
import com.zjlanyun.erp.rfid.m10.u8.reader.server.ReaderBase;
import com.zjlanyun.erp.rfid.m10.u8.reader.server.ReaderHelper;
import com.zjlanyun.erp.view.BaseSwipeBackActivity;


public class DetectionTemperatureActivity extends BaseSwipeBackActivity {

    private TextView temperatureText;
    private Button get;

    private ReaderHelper mReaderHelper;
    private ReaderBase mReader;

    private static ReaderSetting m_curReaderSetting;

    private LocalBroadcastManager lbm;

    @Override
    protected void onResume() {
        if (mReader != null) {
            if (!mReader.IsAlive())
                mReader.StartWait();
        }
        super.onResume();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.rfid_detection_temperature);
        ((MainApplication) getApplication()).addActivity(this);
        initBaseView();

        title.setText("温度检测");
        get = (Button) findViewById(R.id.get);
        temperatureText = (TextView) findViewById(R.id.temperature_text);

        try {
            mReaderHelper = ReaderHelper.getDefaultHelper();
            mReader = mReaderHelper.getReader();
        } catch (Exception e) {
            e.printStackTrace();
        }

        m_curReaderSetting = mReaderHelper.getCurReaderSetting();

        get.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                temperatureText.setText("");
                mReader.getReaderTemperature(m_curReaderSetting.btReadId);
            }
        });

        lbm  = LocalBroadcastManager.getInstance(this);

        IntentFilter itent = new IntentFilter();
        itent.addAction(ReaderHelper.BROADCAST_WRITE_LOG);
        itent.addAction(ReaderHelper.BROADCAST_REFRESH_READER_SETTING);
        lbm.registerReceiver(mRecv, itent);
    }

    /**
     * 显示到界面上
     */
    public void updateView() {
        String strTemperature = "";
        if (m_curReaderSetting.btPlusMinus == 0x0) {
            strTemperature = "-" + String.valueOf(m_curReaderSetting.btTemperature & 0xFF) + "℃";
        } else {
            strTemperature = String.valueOf(m_curReaderSetting.btTemperature & 0xFF) + "℃";
        }

        temperatureText.setText(strTemperature);
    }

    private final BroadcastReceiver mRecv = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(ReaderHelper.BROADCAST_REFRESH_READER_SETTING)) {
                byte btCmd = intent.getByteExtra("cmd", (byte) 0x00);
                if (btCmd == CMD.GET_READER_TEMPERATURE) {
                    updateView();
                }
            } else if (intent.getAction().equals(ReaderHelper.BROADCAST_WRITE_LOG)) {
            }
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (lbm != null)
            lbm.unregisterReceiver(mRecv);
    }
}
